﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace CommonRepositoryInterface.Linq2Sql
{
    public class Linq2SqlRepository<T> : IRepository<T>
        where T : class, IDataRecord
    {
        public DataContext Context { get; private set; }
        public IRepositoryProvider Provider { get; private set; }
        public Linq2SqlRepository(IRepositoryProvider provider, DataContext context)
        {
            this.Context = context;
            this.Provider = provider;
        }

        public IQueryable<T> Query
        {
            get { return Context.GetTable<T>(); }
        }

        public void Insert(T item)
        {
            item.DateCreated = DateTime.UtcNow;
            item.DateModified = item.DateCreated;
            Context.GetTable<T>().InsertOnSubmit(item);
        }

        public void Delete(T item)
        {
            Context.GetTable<T>().DeleteOnSubmit(item);
        }

        public void Update(T item)
        {
            item.DateModified = DateTime.UtcNow;
        }
    }
}
